package com.koron.log.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.business.log.entity.po.LogOperatePo;
import com.koron.log.mapper.LogOperateMapper;
import com.koron.log.service.LogOperateService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * (LogOperate)表服务实现类
 *
 * @author zhouj
 * @since 2022-06-08 11:53:55
 */
@Service("logOperateService")
public class LogOperateServiceImpl implements LogOperateService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(LogOperatePo logOperate) {
        clearBody(Collections.singletonList(logOperate));
        try (SessionFactory factory = new SessionFactory()) {
            LogOperateMapper mapper = factory.getMapper(LogOperateMapper.class);
            return mapper.insertOrUpdate(logOperate);
        }
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(List<LogOperatePo> logOperateList) {
        clearBody(logOperateList);
        try (SessionFactory factory = new SessionFactory()) {
            LogOperateMapper mapper = factory.getMapper(LogOperateMapper.class);
            return mapper.insertOrUpdateBatch(logOperateList);
        }
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(LogOperatePo logOperate) {
        clearBody(Collections.singletonList(logOperate));
        try (SessionFactory factory = new SessionFactory()) {
            LogOperateMapper mapper = factory.getMapper(LogOperateMapper.class);
            return mapper.insert(logOperate);
        }
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(List<LogOperatePo> logOperateList) {
        clearBody(logOperateList);
        try (SessionFactory factory = new SessionFactory()) {
            LogOperateMapper mapper = factory.getMapper(LogOperateMapper.class);
            return mapper.insertBatch(logOperateList);
        }
    }

    /**
     * 数据信息过大，不予保存
     */
    public void clearBody(List<LogOperatePo> logOperateList) {
        if (logOperateList == null) {
            return;
        }
        for (LogOperatePo logOperatePo : logOperateList) {
            logOperatePo.setRequestBoby(null);
        }
    }

    @Override
    @TaskAnnotation("update")
    public int update(LogOperatePo logOperate) {
        try (SessionFactory factory = new SessionFactory()) {
            LogOperateMapper mapper = factory.getMapper(LogOperateMapper.class);
            return mapper.update(logOperate);
        }
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(String id) {
        try (SessionFactory factory = new SessionFactory()) {
            LogOperateMapper mapper = factory.getMapper(LogOperateMapper.class);
            return mapper.deleteById(id);
        }
    }

    @Override
    @TaskAnnotation("queryById")
    public LogOperatePo queryById(String id) {
        try (SessionFactory factory = new SessionFactory()) {
            LogOperateMapper mapper = factory.getMapper(LogOperateMapper.class);
            return mapper.queryById(id);
        }
    }

    @Override
    @TaskAnnotation("queryList")
    public List<LogOperatePo> queryList(LogOperatePo logOperate) {

        try (SessionFactory factory = new SessionFactory()) {
            LogOperateMapper mapper = factory.getMapper(LogOperateMapper.class);
            return mapper.queryList(logOperate);
        }
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(LogOperatePo logOperate, PageQuery pageQuery) {

        try (SessionFactory factory = new SessionFactory()) {
            PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
            LogOperateMapper mapper = factory.getMapper(LogOperateMapper.class);

            List<LogOperatePo> list = mapper.queryList(logOperate);

            return new PageInfo(list);
        }
    }

}